<?

define('AUTH_USER_EXISTS',        1);
define('AUTH_REGISTER_SUCCESS',   2);
define('AUTH_ACCESS_DENIED',      3);
define('AUTH_USER_NOT_FOUND',     4);
define('AUTH_ACTICODE_INVALID',   5);
define('AUTH_ACTICODE_VALID',     6);
define('AUTH_LAST_ADMIN_ACCOUNT', 7);
define('AUTH_PERIOD_INVALID',	  8);
define('AUTH_ADMIN_BLOCK',	  9);


class Auth
{
	public $encriptPassword=true;
	
	function __construct()	
	{
		//$this->RIAUser = new UserManager();
		//$this->session = new SessionManager();
	}	
	
	function login($email,$password)
	{
		if (empty($email)||empty($password))
		{
			return AUTH_ACCESS_DENIED;
		}
		$this->RIAUser->setParam('email',$email);
		if ($this->RIAUser->get(array('getBy' => 'email'))) {
			
			
			if ($this->RIAUser->isBlocked) {
				return AUTH_PERIOD_INVALID;
			}  elseif ($this->RIAUser->BlockedByAdmin($email)){
				return AUTH_ADMIN_BLOCK;
			}
				else {
				if ($this->encriptPass($password) == $this->RIAUser->getParam('password')) {
					if ($_POST['remember_me']) {
						$this->session->clientID = $this->RIAUser->getParam('id');
						$this->session->addKey();
					}
					
					#print "person_id = ".$this->session->clientID;
					#exit();
					$this->loginUser();
					return AUTH_REGISTER_SUCCESS;
				} else {
					return AUTH_USER_NOT_FOUND;
				}
			}
		} else {
			return AUTH_USER_NOT_FOUND;
		}
		
	}
	
	function loginUser() {	
		$_SESSION['person_id']=$this->RIAUser->getParam('id');
		$_SESSION['person_name']=$this->RIAUser->getParam('fio');
//		$_SESSION['person_sex']=$this->RIAUser->getParam('sex');
		$_SESSION['person_info']=$this->RIAUser->client;
	}
	
	function updateClientInfo() {
		$this->RIAUser->setParam('id',$_SESSION['person_id']);
		$this->RIAUser->setClientArray();
		$_SESSION['person_info'] = $this->RIAUser->client;
	}
	
	function isLogged() {
		return $_SESSION['person_id'];
	}
	
	function checkIsLogged() {
		/*if (!$this->isLogged()) {
			if ($this->session->checkKey()) {
				$this->RIAUser->setParam('id',$this->session->clientID);
				if ($this->RIAUser->get(array('getBy' => 'id'))) {
					$this->loginUser();
				}
			}
		}*/
	}

	function encriptPass($password) {
		if ($this->encriptPassword)
		{
			return md5($password);
		}else{
			return $password;
		}
	}
	
	function logout() {
	 	$_SESSION['person_id']=0;
	 	$_SESSION['admin_id']=0;
	 	$_SESSION['person_name']='';
	 	$_SESSION['person_info']='';
	 	$this->session->sessionClose();
	}
	
}
?>